<?php
/**
 * AAService
 * @author fusq
 */
require_once '../app/constants/ConfigConstants.php';

class AAService {

	/**
	 * judje the user if login
	 *
	 * @param boolean $header , if true then header the login page , default is false then return boolean
	 * @return boolean or header the login page
	 */
    public static function isLogin($header = false){
        if(!isset($_SESSION['customerId']) || empty($_SESSION['customerId'])){
        	if($header){
                header("Location: /u/login");
                exit();
        	}
        	return false;
        }
        $customerId = base64_decode($_SESSION['customerId']);
        if($customerId > 0){
            return true;
        }else{
            if($header){
                header("Location: /u/login");
                exit();
            }
            return false;
        }
    }

    public static function checkLogin(){
        $result = AAService::isLogin();
        if($result == false){
            header("Location: ".$GLOBALS["SSLProtocol"]."://".ConfigConstants::$domainPath."/u/select");
            exit();
        }
        return true;
    }

    public static function checkLoginForCheckout(){
        $result = AAService::isLogin();
        if($result == false){
            header("Location: ".$GLOBALS["SSLProtocol"]."://".ConfigConstants::$domainPath."/u/select");
            exit();
        }
        return true;
    }

    public static function getCustomer(){
        return base64_decode($_SESSION['customerId']);
    }

    public static function setCustomer($customerId, $certf = "1", $username, $parentId, $rank, $campusId = 0, $status = 0){
        $_SESSION['customerId'] = base64_encode($customerId);
        $_SESSION['certf'] = $certf;
        $_SESSION['username'] = $username;
        $_SESSION['parentId'] = $parentId;
        $_SESSION['rank'] = $rank;
        $_SESSION['campusId'] = $campusId;
        $_SESSION['status'] = $status;
    }
    
    public static function setStatus($status = 0){
        $_SESSION['status'] = $status;
    }
    
    //判断当前用户状态是否激活
    public static function isActive(){
        if(empty($_SESSION['status'])){//当前用户未激活
            return false;
        }else{//用户已经激活
            return true;
        }
    }

    public static function isValidateForHttps(){
//    	$cookieToken = isset($_COOKIE['token']) ? $_COOKIE['token'] : "";
//    	$sessionToken = isset($_SESSION['token']) ? $_SESSION['token'] : "";
//    	if($cookieToken != "" && $sessionToken != "" && $cookieToken == $sessionToken){//if the token not expired and the token is same as session 
//            //set the https protocol save in cookie
//            $token = md5(uniqid(mt_rand(),true));
////            setcookie("token", $token, time() + 60*30, '/', ".".DOMAIN, true); //set the time is 30 minute
//            setcookie("token", $token, time() + 60*30, '/', ".".DOMAIN, false); //false is used in testing environment
//            $_SESSION['token'] = $token;
    		return true;
//    	}else{//set the user to select view to login and destory the session
////    		unset($_SESSION['token']);
//            require_once '../app/models/UserDao.php';
//            $customerId = self::getCustomer();
//			$customerService = new CustomerService();
//	        $customer = $customerService->getCustomerById($customerId);
//	        $email = $customer[0]['email'];
//    		setcookie("email", $email, time() + 3600*24, '/');
//    		//session_destroy();
////    		setcookie("token", "", time() - 60*30, '/', ".".DOMAIN, true);
//    		setcookie("token", "", time() - 60*30, '/', ".".DOMAIN, false);
//    		return false;
//    	}
    }

}

